package com.a01_2021.thousandstimes.instance.controller;


import com.a01_2021.thousandstimes.exception.CustomException;
import com.a01_2021.thousandstimes.general.jwt.JwtTokenUtil;
import com.a01_2021.thousandstimes.instance.entity.WorkLog;
import com.a01_2021.thousandstimes.instance.service.IWorkLogService;
import com.a01_2021.thousandstimes.utils.AjaxResponse;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author E1xP
 * @since 2021-03-10
 */
@RestController
@RequestMapping("/workLog")
public class WorkLogController {

    @Resource
    IWorkLogService workLogService;

    @Resource
    JwtTokenUtil jwtTokenUtil;

    /**
     * 通过用户id获取工作任务
     *
     * @param userId 用户id
     * @return AjaxResponse
     * @throws CustomException 当用户不存在时抛出该异常
     */
    @PreAuthorize("hasAuthority('workLog-getAllWorkLog')")
    @DS("#header.instanceid")
    @PostMapping("getAllWorkLog")
    public AjaxResponse getAllWorkLog(@RequestHeader("instanceid") String instanceId, long userId) throws CustomException {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SaaS账户访问");
        }
        List<WorkLog> workLogList = workLogService.getAllWorkLog(userId);
        //返回信息封装
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("userId", userId);
        resultMap.put("workLogList", workLogList);
        return AjaxResponse.success(resultMap);
    }

    /**
     * 添加工作任务
     *
     * @param token       jwt令牌（从令牌中获取用户id
     * @param workPlan    工作计划·
     * @param workRecord  工作记录
     * @param workSummary 工作总结
     * @return AjaxResponse
     */
    @PreAuthorize("hasAuthority('workLog-addWorkLog')")
    @DS("#header.instanceid")
    @PostMapping("addWorkLog")
    public AjaxResponse addWorkLog(@RequestHeader("instanceid") String instanceId, @RequestHeader("${jwt.header}") String token, String workPlan, String workRecord, String workSummary) {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SaaS账户访问");
        }
        long userId = Long.parseLong(jwtTokenUtil.getUserIdFromToken(token));//获取用户id
        WorkLog workLog = workLogService.addWorkLog(userId, workPlan, workRecord, workSummary);
        return AjaxResponse.success(workLog);
    }

    /**
     * 给指定用户添加工作任务
     *
     * @param instanceId  实例id
     * @param userId      用户id
     * @param workPlan    工作计划
     * @param workRecord  工作记录
     * @param workSummary 工作总结
     * @return AjaxResponse
     * @throws CustomException 当用户不存在时抛出该异常
     */
    @PreAuthorize("hasAuthority('workLog-addWorkLogToUser')")
    @DS("#header.instanceid")
    @PostMapping("addWorkLogToUser")
    public AjaxResponse addWorkLogToUser(@RequestHeader("instanceid") String instanceId, long userId, String workPlan, String workRecord, String workSummary) throws CustomException {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SaaS账户访问");
        }
        WorkLog workLog = workLogService.addWorkLogToUser(userId, workPlan, workRecord, workSummary);
        return AjaxResponse.success(workLog);

    }

    /**
     * 修改工作任务内容
     *
     * @param token       jwt令牌
     * @param id          工作任务id
     * @param workPlan    工作计划（可选
     * @param workRecord  工作记录（可选
     * @param workSummary 工作总结（可选
     * @return AjaxResponse
     * @throws CustomException 当工作任务不属于请求用户时抛出该异常
     */
    @PreAuthorize("hasAuthority('workLog-updateWorkLog')")
    @DS("#header.instanceid")
    @PostMapping("updateWorkLog")
    public AjaxResponse updateWorkLog(@RequestHeader("instanceid") String instanceId, @RequestHeader("${jwt.header}") String token, long id, String workPlan, String workRecord, String workSummary) throws CustomException {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SaaS账户访问");
        }
        long userId = Long.parseLong(jwtTokenUtil.getUserIdFromToken(token));
        WorkLog workLog = workLogService.updateWorkLog(id, userId, workPlan, workRecord, workSummary);
        return AjaxResponse.success(workLog);
    }

    /**
     * 完成任务
     *
     * @param token jwt令牌
     * @param id    完成的任务id
     * @return AjaxResponse
     * @throws CustomException 当 用户不存在,工作日志不属于该用户 时抛出
     */
    @PreAuthorize("hasAuthority('workLog-finishWorkLog')")
    @DS("#header.instanceid")
    @PostMapping("finishWorkLog")
    public AjaxResponse finishWorkLog(@RequestHeader("instanceid") String instanceId, @RequestHeader("${jwt.header}") String token, long id) throws CustomException {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SaaS账户访问");
        }
        long userId = Long.parseLong(jwtTokenUtil.getUserIdFromToken(token));
        WorkLog workLog = workLogService.finishWorkLog(id, userId);
        return AjaxResponse.success(workLog);
    }
}
